<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增优惠券')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: ztree-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-coupon-add">
        <div class="form-group">
            <label class="col-sm-3 control-label">优惠券名称：</label>
            <div class="col-sm-8">
                <input name="title" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">优惠券类型：</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('coupon_type')}">
                    <input type="radio" th:id="${dict.dictCode}" name="type" th:value="${dict.dictValue}">
                    <label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">优惠券模块类型：</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('model_type')}">
                    <input type="radio" th:id="${dict.dictCode}" name="modelType" th:value="${dict.dictValue}">
                    <label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">优惠券图片：</label>
            <div class="col-sm-8">
                <button type="button" onclick="selectFile() ">选择URL</button>
                <input class="form-control" type="hidden" name="fileId" id="fileId">
                <button type="button" onclick="checkFile() ">检查图片</button>
                <input class="form-control" type="file" name="pic" id="pic" onchange="picUplod('coupon')"/>
                <img id="picImg" name="picImg" style="display: block;"/>
                <input class="form-control" type="hidden" name="imageUrl" id="image"/>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">使用门槛(满X元使用）：</label>
            <div class="col-sm-8">
                <input name="reachAmount" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">面额：</label>
            <div class="col-sm-8">
                <input name="faceValue" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">超过门槛(多少元以上不能使用)：</label>
            <div class="col-sm-8">
                <input name="overAmount" class="form-control" type="text" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">折扣值：</label>
            <div class="col-sm-8">
                <input name="discount" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">总发放量：</label>
            <div class="col-sm-8">
                <input name="totalIssuance" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">已领取数量：</label>
            <div class="col-sm-8">
                <input name="totalGet" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">已使用数量：</label>
            <div class="col-sm-8">
                <input name="totalUse" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">领取开始时间：</label>
            <div class="col-sm-8">
                <div class="input-group date">
                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    <input name="getStartTime" class="form-control" placeholder="yyyy-MM-dd" type="text">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">领取结束时间：</label>
            <div class="col-sm-8">
                <div class="input-group date">
                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    <input name="getEndTime" class="form-control" placeholder="yyyy-MM-dd" type="text">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">使用开始时间：</label>
            <div class="col-sm-8">
                <div class="input-group date">
                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    <input name="useStartTime" class="form-control" placeholder="yyyy-MM-dd" type="text">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">使用结束时间：</label>
            <div class="col-sm-8">
                <div class="input-group date">
                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    <input name="useEndTime" class="form-control" placeholder="yyyy-MM-dd" type="text">
                </div>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">状态：</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('coupon_status')}">
                    <input type="radio" th:id="${dict.dictCode}" name="couponStatus" th:value="${dict.dictValue}">
                    <label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">是否允许分享领取：</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('is_enable')}">
                    <input type="radio" th:id="${dict.dictCode}" name="isAbleShareGet" th:value="${dict.dictValue}">
                    <label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">是否注册赠送：</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('is_enable')}">
                    <input type="radio" th:id="${dict.dictCode}" name="isAbleActivity" th:value="${dict.dictValue}">
                    <label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">是否可用：</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('is_enable')}">
                    <input type="radio" th:id="${dict.dictCode}" name="isEnable" th:value="${dict.dictValue}">
                    <label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <textarea id="remark" name="remark" class="form-control"></textarea>
            </div>
        </div>

        <div class="form-group">
            <span style="color: red" class="col-sm-12 help-block m-b-none"><i class="fa fa-info-circle">
            </i>通用选项不需要额外操作，材料和材料分类选项需要选择多个材料，部分材料可能存在显示bug，导致不会被分到正确的材料分类下，
                所以建议一个个下面，不要使用一键全选功能。处理数据后，请点击编辑查看数据选择是否正确。</span>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">关联子模型：</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('coupon_sub_type')}">
                    <input type="radio" th:id="${dict.dictCode}" name="subType" th:value="${dict.dictValue}">
                    <label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>

        <!-- 编辑关联材料 -->
        <div class="form-group" id="conMaterial" style="display: none">
            <label class="col-sm-3 control-label">关联材料</label>
            <div class="col-sm-8">
                <div id="deptTrees" class="ztree"></div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<th:block th:include="include :: ztree-js" />
<script type="text/javascript">
    var prefix = ctx + "back/coupon";


    $(function() {
        var url = prefix + "/materialTreeData";
        var options = {
            id: "deptTrees",
            url: url,
            check: { enable: true, nocheckInherit: true, chkboxType: { "Y": "ps", "N": "ps" } },
            expandLevel: 2
        };
        $.tree.init(options);
    });

    $('input').on('ifChecked', function (event) {
        if ($(event.target).attr("name") == "subType") {
            var radio = $(event.target).val();
            if (radio == "0") {
                $("#conMaterial").hide();
            } else if (radio == "1") {
                $("#conMaterial").show();
            }
        }
    });

    $("input[name='getStartTime']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    $("input[name='getEndTime']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    $("input[name='useStartTime']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    $("input[name='useEndTime']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            add();
        }
    }
    /*
        * id,title,type,modelType,reachAmount,faceValue,discount,totalIssuance,
        * totalGet,totalUse,getStartTime,getEndTime,useStartTime,useEndTime,couponStatus,isAbleShareGet,isEnable,subType
        *
        * */
    function add() {
        var id = $("input[name='id']").val();
        var title = $("input[name='title']").val();
        var type = $("input[name='type']:checked").val();
        var modelType = $("input[name='modelType']:checked").val();
        var imageUrl = $("input[name='imageUrl']").val();
        var reachAmount = $("input[name='reachAmount']").val();
        var faceValue = $("input[name='faceValue']").val();
        var discount = $("input[name='discount']").val();
        var totalIssuance = $("input[name='totalIssuance']").val();
        var totalGet = $("input[name='totalGet']").val();
        var totalUse = $("input[name='totalUse']").val();
        var getStartTime = $("input[name='getStartTime']").val();
        var getEndTime = $("input[name='getEndTime']").val();
        var useStartTime = $("input[name='useStartTime']").val();
        var useEndTime = $("input[name='useEndTime']").val();
        var couponStatus = $("input[name='couponStatus']:checked").val();
        var isAbleShareGet = $("input[name='isAbleShareGet']:checked").val();
        var isAbleActivity = $("input[name='isAbleActivity']:checked").val();
        var isEnable = $("input[name='isEnable']:checked").val();
        var remark = $("#remark").val();
        var subType = $("input[name='subType']:checked").val();

        var materialIds = $.tree.getCheckedNodes();
        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/add",
            data: {
                "id": id,
                "title": title,
                "type": type,
                "modelType": modelType,
                "reachAmount": reachAmount,
                "imageUrl": imageUrl,
                "faceValue": faceValue,
                "discount": discount,
                "totalIssuance": totalIssuance,
                "totalGet": totalGet,
                "totalUse": totalUse,
                "getStartTime": getStartTime,
                "getEndTime": getEndTime,
                "useStartTime": useStartTime,
                "useEndTime": useEndTime,
                "couponStatus": couponStatus,
                "isAbleShareGet": isAbleShareGet,
                "isAbleActivity": isAbleActivity,
                "isEnable": isEnable,
                "remark": remark,
                "subType": subType,

                "materialIds": materialIds
            },
            async: false,
            error: function (request) {
                $.modal.alertError("系统错误");
            },
            success: function (data) {
                $.operate.successCallback(data);
            }
        });
    }
</script>
</body>
</html>